package com.crazecoder.flutter.iflyad.widget;

import android.content.Context;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.crazecoder.flutter.iflyad.BuildConfig;
import com.crazecoder.flutter.iflyad.R;
import com.crazecoder.flutter.iflyad.utils.OAIDUtil;
import com.iflytek.voiceads.IFLYNativeAd;
import com.iflytek.voiceads.IFLYSplashAd;
import com.iflytek.voiceads.config.AdError;
import com.iflytek.voiceads.config.AdKeys;
import com.iflytek.voiceads.listener.IFLYSplashListener;

import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class SplashADFactory extends PlatformViewFactory implements IFLYSplashListener {
    private IFLYSplashAd mIFLYSplashAd;

    private FrameLayout viewGroup;

    private String adUnitId;

    private MethodChannel channel;

    public SplashADFactory(MessageCodec<Object> createArgsCodec, BinaryMessenger binaryMessenger) {
        super(createArgsCodec);
        channel = new MethodChannel(binaryMessenger, "flutter_iflyad/SplashADFactory");
    }

    @Override
    public PlatformView create(final Context context, int i, final Object o) {


        return new PlatformView() {
            @Override
            public View getView() {
                if (viewGroup != null) return viewGroup;
                Map<String, Object> param = (Map<String, Object>) o;
                int countDown = 5;
                if (param.containsKey("adUnitId")) {
                    adUnitId = param.get("adUnitId").toString();
                }
                if (param.containsKey("countDown")) {
                    countDown = (int) param.get("countDown");
                }
                viewGroup = new FrameLayout(context);
                viewGroup.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                fetchSplashAD(context, viewGroup, SplashADFactory.this, countDown);
                return viewGroup;
            }

            @Override
            public void dispose() {
                viewGroup.removeAllViews();
                viewGroup = null;
            }
        };
    }

    /**
     * 拉取开屏广告，开屏广告的构造方法有3种，详细说明请参考开发者文档。
     *
     * @param context             展示广告的context
     * @param adContainer         展示广告的大容器
     * @param mIFLYSplashListener 广告状态监听器
     * @param countDown           倒计时。
     */
    private void fetchSplashAD(Context context, ViewGroup adContainer, IFLYSplashListener mIFLYSplashListener, int countDown) {
        mIFLYSplashAd = new IFLYSplashAd(context, adUnitId, mIFLYSplashListener);
        //设置oaid，获取方式见 -> 4.常见问题
        String oaid = OAIDUtil.getInstance(context).getOAID();
        mIFLYSplashAd.setParameter(AdKeys.OAID, oaid);
        mIFLYSplashAd.setParameter(AdKeys.COUNT_DOWN, countDown);
        mIFLYSplashAd.setParameter(AdKeys.DEBUG_MODE, BuildConfig.DEBUG);
        mIFLYSplashAd.loadAndShowAd(adContainer);
    }

    @Override
    public void onConfirm() {
    }

    @Override
    public void onCancel() {
    }

    @Override
    public void onAdExposure() {
        //广告曝光
    }

    @Override
    public void onAdSkip() {
        //广告跳过
        channel.invokeMethod("onAdSkip", null);
    }

    @Override
    public void onAdClick() {
        //广告点击
    }

    @Override
    public void onAdTimeOver() {
        //广告倒计时结束
        channel.invokeMethod("onAdTimeOver", null);
    }

    @Override
    public void onAdLoaded() {
        //此回调配合loadAd
        channel.invokeMethod("onAdLoaded", null);
    }

    @Override
    public void onAdFailed(AdError error) {
        Log.e("AdError", error.getErrorDescription());
        //广告获取失败，跳转主页
        channel.invokeMethod("onAdFailed", null);
    }

}